# -*- coding: UTF-8 -*-
# @Time : 2021-01-26 21:02
# @File : Appium.py
import time

from config import Conf
from utils.YmlReadel import YmlReadel
from common.BasePage import Action, screenshot_allure
from utils.LogUtil import my_log
from data.ExcelConfig import TestSteps, Elements, CaseData, TestCases
import allure
from common import MiniProgram
"""
# 获取data数据文件路径
data = Data(Conf.get_data_file())
# 执行测试用例列表
run_list = data.run_list()
"""
# 日志
log = my_log('_APPIUM')


class Appium:
    def __init__(self, driver):
        self.driver = driver

    def test_get_keyworld(self, name):
        """
        #通过映射获取keyworld.yml文件参数

        Conf.get_config_keyworld()  获取keyworld.yml文件路径
        :param name:
        :return:
        """
        reader = YmlReadel(Conf.get_config_keyworld()).data()
        # print(reader)
        value = reader[name]
        return value

    def test_str_to_dict(self, content):
        # 3、获取内容转换为字典，通过“， =”来分割
        res = {}
        try:
            for i in str(content).split(","):
                c = i.split('=')
                res[c[0]] = c[1]
        except Exception as e:
            log.error('错误的send_value：{}'.format(e))
        return res

    def step_run(self, run_case):
        log.info('执行用例内容：{}'.format(run_case))
        self.test_step(run_case)

    @screenshot_allure
    def test_step(self, data, run_case):
        """
        run_case[TestSteps.STEP_TC_ID]  获取TestSteps表STEP_TC_ID = "TC_ID"
        data.get_steps_by_tc_id(tc_id)  根据tc_id获取步骤列表TestSteps相对应的数据
        step[TestSteps.STEP_ELEMENT_NAME]   获取TestSteps表STEP_ELEMENT_NAME元素定位名称
        data.get_ele_by_element(step[TestSteps.STEP_TC_ID], elements)
            -elements =》step[TestSteps.STEP_ELEMENT_NAME]
            -step[TestSteps.STEP_TC_ID] 获取TestSteps表STEP_TC_ID
            -data.get_ele_by_element()  获取elements表元素信息
        test_get_keyworld(step[TestSteps.STEP_OPERATE]) -》operate   映射TestSteps表STEP_OPERATE操作
        getattr(Action(self.driver), operate)   getattr()内置函数
            -Action(self.driver)对象
            -operate字符串
        element[Elements.ELE_BY]    定位类型，如：id，xpath，classname,css,class
        element[Elements.ELE_VALUE] 元素信息
        step[TestSteps.STEP_DATA]   TestSteps表STEP_DATA数据（输入框输入text数据）
        run_case[CaseData.DATA_EXPECT_RESULT]   期望结果
        send[send_value]    TestSteps表数据和CaseData表测试数据对应判断
        :param data:
        :param run_case:
        :return:
        """
        tc_id = run_case[TestSteps.STEP_TC_ID]
        steps = data.get_steps_by_tc_id(tc_id)
        # allure报告信息
        allure.dynamic.feature(run_case[TestCases.CASE_NOTE])
        allure.dynamic.story(run_case[TestCases.CASES_DESC])
        allure.dynamic.title(run_case[CaseData.DATA_CASE_ID] + '-' + run_case[CaseData.DATA_CASE_NAME])
        for step in steps:
            log.debug('TestSteps执行步骤 {}'.format(step))
            elements = step[TestSteps.STEP_ELEMENT_NAME]
            element = data.get_ele_by_element(step[TestSteps.STEP_TC_ID], elements)
            log.debug('Elements元素信息 {}'.format(element))
            operate = self.test_get_keyworld(step[TestSteps.STEP_OPERATE])
            if operate:
                # 定义方法参数：字典,获取by,value,text,verify_toast  使用**kwargs参数格式
                param_value = dict()
                action_methon = getattr(Action(self.driver), operate)
                log.debug('操作步骤关键字{}'.format(operate))
                # driver.find_element(by, value).click()
                by = element[Elements.ELE_BY]
                value = element[Elements.ELE_VALUE]
                send_value = step[TestSteps.STEP_DATA]
                expect = run_case[CaseData.DATA_EXPECT_RESULT]
                param_value['by'] = by
                param_value['value'] = value
                param_value['expect'] = expect
                # param_value['timeout'] = timeout
                # param_value['poll_frequency'] = poll_frequency
                if send_value:
                    data_input = run_case[CaseData.DATA_VALUE]
                    send = self.test_str_to_dict(data_input)
                    param_value['send'] = send[send_value]
                # step
                with allure.step(step[TestSteps.STEP_NAME]):
                    action_methon(**param_value)
            else:
                log.error('没有operate操作信息{}'.format(operate))



